package com.hscy.push.util;

import com.corundumstudio.socketio.SocketIOClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author xuteng
 */
public class ClientCacheUtil {

    /**
     * 本地缓存已连接的客户端
     */
    private static final Map<String, HashMap<UUID, SocketIOClient>> concurrentHashMap = new ConcurrentHashMap<>();

    /**
     * 存入本地缓存
     *
     * @param userId         用户ID
     * @param sessionId      页面sessionID
     * @param socketIOClient 页面对应的通道连接信息
     */
    public static void saveClient(String userId, UUID sessionId, SocketIOClient socketIOClient) {
        HashMap<UUID, SocketIOClient> sessionIdClientCache = concurrentHashMap.get(userId);
        if (sessionIdClientCache == null) {
            sessionIdClientCache = new HashMap<>();
        }
        sessionIdClientCache.put(sessionId, socketIOClient);
        concurrentHashMap.put(userId, sessionIdClientCache);
        HashMap<UUID, SocketIOClient> socketIOClientHashMap = concurrentHashMap.get(userId);

        System.out.println(socketIOClientHashMap.size());
    }

    /**
     * 根据用户ID获取所有通道信息
     *
     * @param userId
     * @return
     */
    public static HashMap<UUID, SocketIOClient> getUserClient(String userId) {
        return concurrentHashMap.get(userId);
    }


    /**
     * 根据多个用户ID获取所有通道信息
     *
     * @param userIdList
     * @return
     */
    public static HashMap<UUID, SocketIOClient> getUserClient(List<String> userIdList) {

        HashMap<UUID, SocketIOClient> socketIOClientHashMap = new HashMap<>();
        for (String userId : userIdList) {
            concurrentHashMap.computeIfAbsent(userId, k -> new HashMap<>());
            socketIOClientHashMap.putAll(concurrentHashMap.get(userId));
        }
        return socketIOClientHashMap;
    }

    /**
     * 根据用户ID及页面sessionID删除页面链接信息
     *
     * @param userId
     * @param sessionId
     */
    public static void deleteSessionClient(String userId, UUID sessionId) {
        concurrentHashMap.get(userId).remove(sessionId);
    }

}
